package com.ruoyi.shortplay.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shortplay.domain.MyChannelVideo;
import com.ruoyi.shortplay.domain.YoutobeDateOpera;
import com.ruoyi.shortplay.mapper.MyChannelVideoMapper;
import com.ruoyi.shortplay.mapper.YoutobeDateOperaMapper;
import com.ruoyi.shortplay.service.IYoutobeDateOperaService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.youtobe.domain.YoutobeChannelInfo;
import com.ruoyi.youtobe.mapper.YoutobeChannelInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 每日操作Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-18
 */
@Slf4j
@Service
public class YoutobeDateOperaServiceImpl implements IYoutobeDateOperaService {
    @Resource
    private YoutobeDateOperaMapper youtobeDateOperaMapper;

    /**
     * 查询每日操作
     *
     * @param id 每日操作主键
     * @return 每日操作
     */
    @Override
    public YoutobeDateOpera selectYoutobeDateOperaById(Long id) {
        return youtobeDateOperaMapper.selectYoutobeDateOperaById(id);
    }

    @Resource
    SysUserMapper sysUserMapper;
    /**
     * 查询每日操作列表
     *
     * @param youtobeDateOpera 每日操作
     * @param userId
     * @return 每日操作
     */
    @Override
    public List<YoutobeDateOpera> selectYoutobeDateOperaList(YoutobeDateOpera youtobeDateOpera, Long userId) {
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        if (sysUser!=null&& StringUtils.isNotBlank( sysUser.getChannelIds())) {
            youtobeDateOpera.setOpId(userId);
        }
        return youtobeDateOperaMapper.selectYoutobeDateOperaList(youtobeDateOpera);
    }

    /**
     * 新增每日操作
     *
     * @param youtobeDateOpera 每日操作
     * @return 结果
     */
    @Override
    public int insertYoutobeDateOpera(YoutobeDateOpera youtobeDateOpera) {
        youtobeDateOpera.setCreateTime(DateUtils.getNowDate());
        return youtobeDateOperaMapper.insertYoutobeDateOpera(youtobeDateOpera);
    }

    @Resource
    YoutobeChannelInfoMapper youtobeChannelInfoMapper;
    @Resource
    MyChannelVideoMapper myChannelVideoMapper;
    /**
     * 修改每日操作
     *
     * @return 结果
     */
    @Override
    public AjaxResult updateYoutobeDateOpera(YoutobeDateOpera params) {
        YoutobeChannelInfo channel = new YoutobeChannelInfo();
        if (params.getChannelId() != null) {
            channel = youtobeChannelInfoMapper.selectYoutobeChannelInfoById(params.getChannelId());
            params.setPushChannel(channel.getChannelName());
        }
        params.setUpdateTime(DateUtils.getNowDate());

        int i = youtobeDateOperaMapper.updateYoutobeDateOpera(params);
        //如果状态改为发布，则新增至频道下视频表
        if ("upload".equals(params.getType())) {
            //上传需要上传后的频道ID 和 频道ID
            if (params.getChannelId() == null) {
                // 手动设置回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("关联片单为空，不能分发至操作表！");
            }
            //根据ID获取频道信息 youtobe_channel_info
            YoutobeDateOpera data = youtobeDateOperaMapper.selectYoutobeDateOperaById(params.getId());
            //我管理的频道下的视频表数据新增
            MyChannelVideo video = new MyChannelVideo();
            video.setVideoId(params.getVideoId());
            video.setDefaultTitle(data.getTitle());
            //待解版权: no_copy_right; 不公开列出    no_public; 待发布      no_release;  已发布 release;异常 abnormal
            video.setState("no_copy_right");
            video.setCreateTime(new Date());
            if (channel!=null) {
                video.setChannelId(channel.getId());
                video.setChannelName(channel.getChannelName());
                video.setType(channel.getType());
            }
            video.setOpId(params.getId());
            video.setPcId(data.getPcId());
            video.setVideoId(params.getVideoId());
            video.setPlay(data.getTitle());
            myChannelVideoMapper.insertMyChannelVideo(video);

        }
        return AjaxResult.success("修改成功！");
    }

    /**
     * 批量删除每日操作
     *
     * @param ids 需要删除的每日操作主键
     * @return 结果
     */
    @Override
    public int deleteYoutobeDateOperaByIds(Long[] ids) {
        return youtobeDateOperaMapper.deleteYoutobeDateOperaByIds(ids);
    }

    /**
     * 删除每日操作信息
     * 
     * @param id 每日操作主键
     * @return 结果
     */
    @Override
    public int deleteYoutobeDateOperaById(Long id)
    {
        return youtobeDateOperaMapper.deleteYoutobeDateOperaById(id);
    }
}
